
#include "librga.h"



struct timeval start, end;
long usec1;


const struct format_table_entry format_table[] = {
    { RK_FORMAT_RGBA_8888,          "rgba8888" },
    { RK_FORMAT_RGBX_8888,          "rgbx8888" },
    { RK_FORMAT_RGB_888,            "rgb888" },
    { RK_FORMAT_RGB_565,            "rgb565" },
    { RK_FORMAT_RGBA_5551,          "rgba5551" },
    { RK_FORMAT_RGBA_4444,          "rgba4444" },
    { RK_FORMAT_BGRA_8888,          "bgra8888" },
    { RK_FORMAT_BGRX_8888,          "bgrx8888" },
    { RK_FORMAT_BGR_888,            "bgr888" },
    { RK_FORMAT_BGR_565,            "bgr565" },
    { RK_FORMAT_BGRA_5551,          "bgra5551" },
    { RK_FORMAT_BGRA_4444,          "bgra4444" },

    { RK_FORMAT_YCbCr_422_SP,       "cbcr422sp" },
    { RK_FORMAT_YCbCr_422_P,        "cbcr422p" },
    { RK_FORMAT_YCbCr_420_SP,       "nv12" },
    { RK_FORMAT_YCbCr_420_P,        "crcb420p" },

    { RK_FORMAT_YCrCb_422_SP,       "crcb422sp" },
    { RK_FORMAT_YCrCb_422_P,        "crcb422p" },
    { RK_FORMAT_YCrCb_420_SP,       "crcb420sp" },
    { RK_FORMAT_YCrCb_420_P,        "crcb422p" },

    { RK_FORMAT_BPP1,               "bpp1" },
    { RK_FORMAT_BPP2,               "bpp2" },
    { RK_FORMAT_BPP4,               "bpp4" },
    { RK_FORMAT_BPP8,               "bpp8" },

    { RK_FORMAT_Y4,                 "y4" },
    { RK_FORMAT_YCbCr_400,          "cbcr400" },

    { RK_FORMAT_BGRX_8888,          "bgrx8888" },

    { RK_FORMAT_YVYU_422,           "yvyu422" },
    { RK_FORMAT_YVYU_420,           "yvyuv420" },
    { RK_FORMAT_VYUY_422,           "vyuy422" },
    { RK_FORMAT_VYUY_420,           "vyuy420" },

    { RK_FORMAT_YUYV_422,           "yuyv422" },
    { RK_FORMAT_YUYV_420,           "yuyv420" },
    { RK_FORMAT_UYVY_422,           "uyvy422" },
    { RK_FORMAT_UYVY_420,           "uyvy420" },

    { RK_FORMAT_YCbCr_420_SP_10B,   "nv12_10" },
    { RK_FORMAT_YCrCb_420_SP_10B,   "crcb420sp_10" },
    { RK_FORMAT_YCbCr_422_10b_SP,   "cbcr422_10b" },
    { RK_FORMAT_YCrCb_422_10b_SP,   "crcb422_10b" },

    { RK_FORMAT_BGR_565,            "bgr565" },
    { RK_FORMAT_BGRA_5551,          "bgra5551" },
    { RK_FORMAT_BGRA_4444,          "bgra4444" },

    { RK_FORMAT_ARGB_8888,          "argb8888" },
    { RK_FORMAT_XRGB_8888,          "xrgb8888" },
    { RK_FORMAT_ARGB_5551,          "argb5551" },
    { RK_FORMAT_ARGB_4444,          "argb4444" },
    { RK_FORMAT_ABGR_8888,          "abgr8888" },
    { RK_FORMAT_XBGR_8888,          "xbgr8888" },
    { RK_FORMAT_ABGR_5551,          "abgr5551" },
    { RK_FORMAT_ABGR_4444,          "abgr4444" },

    { RK_FORMAT_UNKNOWN,            "unknown" }
};


int RkRgaCompatibleFormat(int format) {
    if (format == 0)
        return format;

    if ((format >> 8) != 0) {
        return format;
    } else {
        return format << 8;
    }
    return format;
}


float get_bpp_from_format(int format) {
    float bpp = 0;

    if (!(format & 0xFF00 || format == 0)) {
        format = RkRgaCompatibleFormat(format);
    }

    switch (format) {
        case RK_FORMAT_Y4:
            bpp = 0.5;
            break;
        case RK_FORMAT_BPP1:
        case RK_FORMAT_BPP2:
        case RK_FORMAT_BPP4:
        case RK_FORMAT_BPP8:
        case RK_FORMAT_YCbCr_400:
            bpp = 1;
            break;
        case RK_FORMAT_YCbCr_420_SP:
        case RK_FORMAT_YCbCr_420_P:
        case RK_FORMAT_YCrCb_420_P:
        case RK_FORMAT_YCrCb_420_SP:
        /* yuyv */
        case RK_FORMAT_YVYU_420:
        case RK_FORMAT_VYUY_420:
        case RK_FORMAT_YUYV_420:
        case RK_FORMAT_UYVY_420:
            bpp = 1.5;
            break;
        case RK_FORMAT_RGB_565:
        case RK_FORMAT_RGBA_5551:
        case RK_FORMAT_RGBA_4444:
        case RK_FORMAT_BGR_565:
        case RK_FORMAT_BGRA_5551:
        case RK_FORMAT_BGRA_4444:
        case RK_FORMAT_ARGB_5551:
        case RK_FORMAT_ARGB_4444:
        case RK_FORMAT_ABGR_5551:
        case RK_FORMAT_ABGR_4444:
        case RK_FORMAT_YCbCr_422_SP:
        case RK_FORMAT_YCbCr_422_P:
        case RK_FORMAT_YCrCb_422_SP:
        case RK_FORMAT_YCrCb_422_P:
        /* yuyv */
        case RK_FORMAT_YVYU_422:
        case RK_FORMAT_VYUY_422:
        case RK_FORMAT_YUYV_422:
        case RK_FORMAT_UYVY_422:
            bpp = 2;
            break;
        /*RK encoder requires alignment of odd multiples of 256.*/
        /*Here bpp=2 guarantee to read complete data.*/
        case RK_FORMAT_YCbCr_420_SP_10B:
        case RK_FORMAT_YCrCb_420_SP_10B:
            bpp = 2;
            break;
        case RK_FORMAT_YCbCr_422_10b_SP:
        case RK_FORMAT_YCrCb_422_10b_SP:
            bpp = 2.5;
            break;
        case RK_FORMAT_BGR_888:
        case RK_FORMAT_RGB_888:
            bpp = 3;
            break;
        case RK_FORMAT_RGBA_8888:
        case RK_FORMAT_RGBX_8888:
        case RK_FORMAT_BGRA_8888:
        case RK_FORMAT_BGRX_8888:
        case RK_FORMAT_ARGB_8888:
        case RK_FORMAT_XRGB_8888:
        case RK_FORMAT_ABGR_8888:
        case RK_FORMAT_XBGR_8888:
            bpp = 4;
            break;
        default:
            printf("Is unsupport format now, please fix \n");
            return 0;
    }

    return bpp;
}

int RkRgaGetRgaFormat(int format) {
    /* Because the format of librga is the value of driver format << 8 . */

    if (format & 0xFF00 || format == 0)
        return format;
    else {
        format = RkRgaCompatibleFormat(format);
        if (format & 0xFF00 || format == 0)
            return format;
    }

    printf("%x is unsupport format now,pilese fix.", format);
    return -1;
}


const char *translate_format_str(int format) {
    format = RkRgaGetRgaFormat(format);

    for (int i = 0; i < sizeof(format_table) / sizeof(format_table[0]); i++)
        if (format_table[i].format == format)
            return format_table[i].name;

    return "unknown";
}

int get_buf_size_by_w_h_f(int w, int h, int f) {
    float bpp = get_bpp_from_format(f);
    int size = 0;

    size = (int)w * h * bpp;
    return size;
}

int get_buf_from_file(void *buf, int f, int sw, int sh, int index) {


    const char *inputFilePath = "/usr/data/in%dw%d-h%d-%s.bin";

    char filePath[100];
    int ret = 0;

    snprintf(filePath, 100, inputFilePath, index, sw, sh, translate_format_str(f));

    FILE *file = fopen(filePath, "rb");
    if (!file) {
        fprintf(stderr, "Could not open %s\n", filePath);
        return -1;
    }
    fread(buf, get_buf_size_by_w_h_f(sw, sh, f), 1, file);
    fclose(file);

    return 0;
}


int output_buf_data_to_file(void *buf, int f, int sw, int sh, int index) {

    const char *outputFilePath = "/usr/data/out%dw%d-h%d-%s.bin";

    char filePath[100];
    int ret = 0;

	printf("format=%s\n",translate_format_str(f));

    snprintf(filePath, 100, outputFilePath, index, sw, sh, translate_format_str(f));

    FILE *file = fopen(filePath, "wb+");
    if (!file) {
        fprintf(stderr, "Could not open %s\n", filePath);
        return 0;
    } else
        fprintf(stderr, "open %s and write ok\n", filePath);
    fwrite(buf, get_buf_size_by_w_h_f(sw, sh, f), 1, file);
    fclose(file);

    return 0;
}


int print_expected(rga_buffer_t src, rga_buffer_t dst) {
    int ret;
    long volume = 0, time = 0;
    rga_info_table_entry info_table;

    volume = src.wstride*src.hstride*get_bpp_from_format(src.format);
    printf("Expected read data	: %ld bit\n", volume);
    volume = dst.wstride*dst.hstride*get_bpp_from_format(dst.format);
    printf("Expected write data	: %ld bit\n", volume );

    ret = rga_get_info(&info_table);
    if (ret == IM_STATUS_FAILED) {
        printf("RGA get info error\n");
        return -1;
    }

    /* The calculation formula only supports RGA copy mode:
     * Number of pixels / pixels that aclk can process per second */
    time = (src.wstride*src.hstride) / (info_table.performance*300);
    printf("Expected time	 	: %ld us\n", time);

    return 0;
}

int rga_control(int cmd1 ,int cmd2)
{
    int ret = 0, while_time = 0;

    int       		COLOR;
    IM_USAGE  		ROTATE;
    IM_USAGE  		FLIP;

    IM_INFORMATION	IM_INFO;
    IM_STATUS 		STATUS;

    im_rect 		src_rect;
    im_rect 		dst_rect;
    rga_buffer_t 	src;
    rga_buffer_t 	dst;


    char* src_buf = NULL;
    char* dst_buf = NULL;

	memset(&src_rect, 0, sizeof(src_rect));
	memset(&dst_rect, 0, sizeof(dst_rect));
	memset(&src, 0, sizeof(src));
	memset(&dst, 0, sizeof(dst));


	log_i("cmd1 = %x\n", cmd1);
	log_i("cmd2 = %x\n", cmd2);

    /********** Get parameters **********/
    if(cmd1 != MODE_QUERYSTRING) {
        src_buf = (char*)malloc(SRC_WIDTH*SRC_HEIGHT*get_bpp_from_format(SRC_FORMAT));
        dst_buf = (char*)malloc(DST_WIDTH*DST_HEIGHT*get_bpp_from_format(DST_FORMAT));

        ret = get_buf_from_file(src_buf, SRC_FORMAT, SRC_WIDTH, SRC_HEIGHT, 0);
        if (!ret)
            printf("open file\n");
        else
            printf ("can not open file\n");

        if(cmd1 == MODE_BLEND || cmd1 == MODE_FILL) {
            ret = get_buf_from_file(dst_buf, DST_FORMAT, DST_WIDTH, DST_HEIGHT, 1);
            if (!ret)
                printf("open file\n");
            else
                printf ("can not open file\n");
        } else {
            memset(dst_buf,0x00,DST_WIDTH*DST_HEIGHT*get_bpp_from_format(DST_FORMAT));
        }

        src = wrapbuffer_virtualaddr(src_buf, SRC_WIDTH, SRC_HEIGHT, SRC_FORMAT);
        dst = wrapbuffer_virtualaddr(dst_buf, DST_WIDTH, DST_HEIGHT, DST_FORMAT);
        if(src.width == 0 || dst.width == 0) {
            printf("%s, %s\n", __FUNCTION__, imStrError());
            return ERROR;
        }
    }

	do {
		if (while_time) {
			static int while_num = 1;
			printf("This is %d time in the loop\n", while_num);

			while_num++;
			while_time--;
	    }
	    /********** Execution function according to mode **********/
	    switch(cmd1) {
	        case MODE_QUERYSTRING :

	            IM_INFO = (IM_INFORMATION)cmd2;
	            printf("\n%s\n", querystring(IM_INFO));

	            break;

	        case MODE_COPY :      //rgaImDemo --copy

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }
				gettimeofday(&start, NULL);

	            STATUS = imcopy(src, dst);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("copying .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

				print_expected(src, dst);

	            break;

	        case MODE_RESIZE :    //rgaImDemo --resize=up/down

	            switch(cmd2) {
	                case IM_UP_SCALE :

	                    if (dst_buf != NULL) {
	                        free(dst_buf);
	                        dst_buf = NULL;
	                    }
	                    dst_buf = (char*)malloc(1920*1080*get_bpp_from_format(DST_FORMAT));

	                    memset(dst_buf,0x00,1920*1080*get_bpp_from_format(DST_FORMAT));

	                    dst = wrapbuffer_virtualaddr(dst_buf, 1920, 1080, DST_FORMAT);
	                    if(dst.width == 0) {
	                        printf("%s, %s\n", __FUNCTION__, imStrError());
	                        return ERROR;
	                    }

	                    break;
	                case IM_DOWN_SCALE :

	                    if (dst_buf != NULL) {
	                        free(dst_buf);
	                        dst_buf = NULL;
	                    }
	                    dst_buf = (char*)malloc(720*480*get_bpp_from_format(DST_FORMAT));

	                    memset(dst_buf,0x00,720*480*get_bpp_from_format(DST_FORMAT));

	                    dst = wrapbuffer_virtualaddr(dst_buf, 720, 480, DST_FORMAT);
	                    if(dst.width == 0) {
	                        printf("%s, %s\n", __FUNCTION__, imStrError());
	                        return ERROR;
	                    }

	                    break;
	            }

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imresize(src, dst);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("resizing .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_CROP :      //rgaImDemo --crop

	            src_rect.x      = 100;
	            src_rect.y      = 100;
	            src_rect.width  = 300;
	            src_rect.height = 300;

	            ret = imcheck(src, dst, src_rect, dst_rect, IM_CROP);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imcrop(src, dst, src_rect);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("cropping .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_ROTATE :    //rgaImDemo --rotate=90/180/270

	            ROTATE = (IM_USAGE)cmd2;

	            if (IM_HAL_TRANSFORM_ROT_90 ==  ROTATE || IM_HAL_TRANSFORM_ROT_270 == ROTATE) {
	                dst.width   = src.height;
	                dst.height  = src.width;
	                dst.wstride = src.hstride;
	                dst.hstride = src.wstride;
	            }

	            ret = imcheck(src, dst, src_rect, dst_rect, ROTATE);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imrotate(src, dst, ROTATE);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("rotating .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_FLIP :      //rgaImDemo --flip=H/V

	            FLIP = (IM_USAGE)cmd2;

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imflip(src, dst, FLIP);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("flipping .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_TRANSLATE : //rgaImDemo --translate

	            src_rect.x = 300;
	            src_rect.y = 300;

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imtranslate(src, dst, src_rect.x, src_rect.y);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("translating .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_BLEND :     //rgaImDemo --blend

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imblend(src, dst);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("blending .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_CVTCOLOR :  //rgaImDemo --cvtcolor

	            src.format = RK_FORMAT_RGBA_8888;
	            dst.format = RK_FORMAT_YCbCr_420_SP;
			
	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imcvtcolor(src, dst, src.format, dst.format);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("cvtcolor .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_FILL :      //rgaImDemo --fill=blue/green/red

	            COLOR = cmd2;

	            dst_rect.x      = 100;
	            dst_rect.y      = 100;
	            dst_rect.width  = 300;
	            dst_rect.height = 300;

	            ret = imcheck(src, dst, src_rect, dst_rect, IM_COLOR_FILL);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imfill(dst, dst_rect, COLOR);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("filling .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_NONE :

	            printf("%s, Unknown mode\n", __FUNCTION__);

	            break;

	        default :

	            printf("%s, Invalid mode\n", __FUNCTION__);

	            break;
	    }

	    if (while_time) {
			/* 200ms */
			usleep(200000);
	    }
	}while(while_time);

    /********** output buf data to file **********/


    if (src_buf != NULL) {
        free(src_buf);
        src_buf = NULL;
    }

    if (dst_buf != NULL) {
        output_buf_data_to_file(dst_buf, dst.format, dst.wstride, dst.hstride, 0);
        free(dst_buf);
        dst_buf = NULL;
    }

    return 0;
}


int rga_control2(int cmd1 ,char * in_buff, int in_w,int in_h ,int in_bpp,
							char* out_buff,int out_w,int out_h,int out_bpp)
{
    int ret = 0, while_time = 0;

    int       		COLOR;
    IM_USAGE  		ROTATE;
    IM_USAGE  		FLIP;

    IM_INFORMATION	IM_INFO;
    IM_STATUS 		STATUS;

    im_rect 		src_rect;
    im_rect 		dst_rect;
    rga_buffer_t 	src;
    rga_buffer_t 	dst;


    char* src_buf    = in_buff;
    char* dst_buf    = out_buff;

	int   src_width  = in_w;
	int   src_height = in_h;
	int   src_bpp    = in_bpp;

	int   dst_width  = out_w;
	int   dst_height = out_h;
	int   dst_bpp    = out_bpp;

	memset(&src_rect, 0, sizeof(src_rect));
	memset(&dst_rect, 0, sizeof(dst_rect));
	memset(&src, 0, sizeof(src));
	memset(&dst, 0, sizeof(dst));


	//log_i("cmd1 = %x\n", cmd1);

    src = wrapbuffer_virtualaddr(src_buf, src_width, src_height, RK_FORMAT_RGB_888);
    dst = wrapbuffer_virtualaddr(dst_buf, dst_width, dst_height, RK_FORMAT_RGB_888);

	if(src.width == 0 || dst.width == 0)
	{
        log_e("error-%s\n",imStrError());
        return ERROR;
	}

	do {
		if (while_time) {
			static int while_num = 1;
			log_i("This is %d time in the loop\n", while_num);

			while_num++;
			while_time--;
	    }
	    /********** Execution function according to mode **********/
	    switch(cmd1) {
	        case MODE_QUERYSTRING :

	            break;

	        case MODE_COPY :      //rgaImDemo --copy

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }
				gettimeofday(&start, NULL);

	            STATUS = imcopy(src, dst);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("copying .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

				print_expected(src, dst);

	            break;

	        case MODE_RESIZE :    //rgaImDemo --resize=up/down

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imresize(src, dst);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);

				//printf("resizing .... cost time %ld us,%ld ms, %s\n", usec1,usec1/1000, imStrError(STATUS));

	            break;

	        case MODE_CROP :      //rgaImDemo --crop

	            src_rect.x      = 100;
	            src_rect.y      = 100;
	            src_rect.width  = 300;
	            src_rect.height = 300;

	            ret = imcheck(src, dst, src_rect, dst_rect, IM_CROP);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imcrop(src, dst, src_rect);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("cropping .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_ROTATE :    //rgaImDemo --rotate=90/180/270

				dst.width   = src.height;
				dst.height  = src.width;
				dst.wstride = src.hstride;
				dst.hstride = src.wstride;

	            ret = imcheck(src, dst, src_rect, dst_rect, ROTATE);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imrotate(src, dst, ROTATE);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("reotate .... cost time %ld us,%ld ms, %s\n", usec1,usec1/1000, imStrError(STATUS));

	            break;

	        case MODE_FLIP :      //rgaImDemo --flip=H/V

	            FLIP = (IM_USAGE)0;

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imflip(src, dst, FLIP);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("flipping .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_TRANSLATE : //rgaImDemo --translate

	            src_rect.x = 300;
	            src_rect.y = 300;

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imtranslate(src, dst, src_rect.x, src_rect.y);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("translating .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_BLEND :     //rgaImDemo --blend

	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imblend(src, dst);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("blending .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_CVTCOLOR :  //rgaImDemo --cvtcolor

	            src.format = RK_FORMAT_RGBA_8888;
	            dst.format = RK_FORMAT_YCbCr_420_SP;
			
	            ret = imcheck(src, dst, src_rect, dst_rect);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imcvtcolor(src, dst, src.format, dst.format);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("cvtcolor .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_FILL :      //rgaImDemo --fill=blue/green/red

	            COLOR = 0;

	            dst_rect.x      = 100;
	            dst_rect.y      = 100;
	            dst_rect.width  = 300;
	            dst_rect.height = 300;

	            ret = imcheck(src, dst, src_rect, dst_rect, IM_COLOR_FILL);
	            if (IM_STATUS_NOERROR != ret) {
	                printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
	                return -1;
	            }

				gettimeofday(&start, NULL);

	            STATUS = imfill(dst, dst_rect, COLOR);

	            gettimeofday(&end, NULL);
	            usec1 = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	            printf("filling .... cost time %ld us, %s\n", usec1, imStrError(STATUS));

	            break;

	        case MODE_NONE :

	            printf("%s, Unknown mode\n", __FUNCTION__);

	            break;

	        default :

	            printf("%s, Invalid mode\n", __FUNCTION__);

	            break;
	    }

	    if (while_time) {
			/* 200ms */
			usleep(200000);
	    }
	}while(while_time);

    /********** output buf data to file **********/
#if 0

	printf("dst.format=%d\n",dst.format >> 8);

    if (dst_buf != NULL) {
        output_buf_data_to_file(dst_buf, dst.format, dst.wstride, dst.hstride, 0);
    }
#endif

    return 0;
}

